// Copyright (C)2012 Johann Duscher. All rights reserved.
//
// This file is part of QwTAPI.
//
// QwTAPI is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// QwTAPI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with QwTAPI.  If not, see <http://www.gnu.org/licenses/>.

#include "ConnectionTest.h"

#include <qwtapi/Line.h>
#include <qwtapi/Call.h>


using namespace qwtapi;

ConnectionTest::ConnectionTest(QObject *parent)
    : QObject(parent),
      m_pConnection(0)
{
}

ConnectionTest::~ConnectionTest()
{
}

void ConnectionTest::initTestCase()
{
    m_pConnection = Connection::instance();
    QVERIFY(NULL != m_pConnection);

    LONG errCode = m_pConnection->initialize("ConnectionTest", TAPI_CURRENT_VERSION);
    QCOMPARE(errCode, 0L);
}

void ConnectionTest::cleanupTestCase()
{
    m_pConnection->shutdown();
}

void ConnectionTest::ensureDevicesAreFound()
{
    QVERIFY(m_pConnection->getLineDeviceCount() > 0);
}

void ConnectionTest::ensureDeviceWithVoiceCapabilitiyIsFound()
{
    Connection::LineList matchingLines = m_pConnection->findLines(MediaMode::INTERACTIVEVOICE);
    QVERIFY(!matchingLines.isEmpty());
}

void ConnectionTest::makeOutgoingCall()
{
    Connection::LineList matchingLines = m_pConnection->findLines(MediaMode::INTERACTIVEVOICE);

    Line* pLine = matchingLines.first();
    QVERIFY(NULL != pLine);

    LONG result = pLine->open(CallPrivilege::OWNER | CallPrivilege::MONITOR, MediaMode::INTERACTIVEVOICE);
    QCOMPARE(result, 0L);

    result = pLine->makeCall("017678128123");
    QVERIFY(result >= 0);

    QTest::qWait(3 * 1000);

    Line::CallList callList = pLine->getCalls();
    QCOMPARE(callList.size(), 1);

    Call* pCall = callList.first();
    const CallInfo& ci = pCall->getCallInfo(&result);
    QCOMPARE(result, 0L);
    qDebug()
        << ci.getCallerIdName()
        << ci.getCallerIdNumber()
        << ci.getConnectedIdName()
        << ci.getConnectedIdNumber()
        << ci.getCalledIdName()
        << ci.getCalledIdNumber()
        << ci.getRedirectingIdName()
        << ci.getRedirectingIdNumber()
        << ci.getRedirectionIdName()
        << ci.getRedirectionIdNumber();

    pCall->drop();

    QTest::qWait(3 * 1000);

    QCOMPARE(pLine->getCalls().size(), 0);

    pLine->close();
}

void ConnectionTest::receiveIncomingCall()
{
    Connection::LineList matchingLines = m_pConnection->findLines(MediaMode::INTERACTIVEVOICE);

    Line* pLine = matchingLines.first();
    QVERIFY(NULL != pLine);

    LONG result = pLine->open(CallPrivilege::OWNER | CallPrivilege::MONITOR, MediaMode::INTERACTIVEVOICE);
    QCOMPARE(result, 0L);

    QTest::qWait(15 * 1000);

    Line::CallList callList = pLine->getCalls();
    QCOMPARE(callList.size(), 1);

    Call* pCall = callList.first();

    result = pCall->setPrivilege(CallPrivilege::OWNER);
    QCOMPARE(result, 0L);

    const CallInfo& ci = pCall->getCallInfo(&result);
    QCOMPARE(result, 0L);
    qDebug()
        << ci.getCallerIdName()
        << ci.getCallerIdNumber()
        << ci.getConnectedIdName()
        << ci.getConnectedIdNumber()
        << ci.getCalledIdName()
        << ci.getCalledIdNumber()
        << ci.getRedirectingIdName()
        << ci.getRedirectingIdNumber()
        << ci.getRedirectionIdName()
        << ci.getRedirectionIdNumber();

    result = pCall->drop();
    QVERIFY(result >= 0L);

    QTest::qWait(3 * 1000);

    result = pLine->close();
    QCOMPARE(result, 0L);

    QCOMPARE(pLine->getCalls().size(), 0);
}
